package com.netease.nie.base.utils;

import java.time.LocalTime;
import java.util.Calendar;
import java.util.Locale;

/**
 * 
 * @author KingsHunter
 * @createDate June 20th,2016
 * 
 */
public final class DateUtils {

	public static long currentTimeInMills() {
		return System.currentTimeMillis();
	}
	
	public static long currentTimeInSeconds() {
		return currentTimeInMills() / 1000;
	}

	public static int daysBetween(long timeMills1, long timeMills2) {
		if (timeMills1 > timeMills2)
			return 0;
		long timeMills = timeMills2 - timeMills1;
		Long daysBetween = Math.abs(timeMills / (24 * 60 * 60 * 1000));
		return daysBetween.intValue();
	}
	
	public static int secondsBetweenFutureTimeMills(long timeMills) {
		long currentTimeMills = currentTimeInMills();
		if (currentTimeMills >= timeMills)
			return 0;
		long diff = timeMills - currentTimeMills;
		Long secondsBetween = Math.abs(diff / 1000);
		return secondsBetween.intValue();
	}

	public static int secondsBetween(long timeMills1, long timeMills2) {
		if (timeMills1 > timeMills2)
			return 0;
		long timeMills = timeMills2 - timeMills1;
		Long secondsBetween = Math.abs(timeMills / 1000);
		return secondsBetween.intValue();
	}
	
	public static boolean isEqualTimeRange(int date, int hour) {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		int dayOfMonth = c.get(Calendar.DATE);
		int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
		if (date == dayOfMonth && hour == hourOfDay)
			return true;
		return false;
	}
	
	private static int time1CompareTotime2(LocalTime time1, LocalTime time2) {
		return time1.compareTo(time2);
	}
	
	public static int circlePerDaySecondsBetween(int secondOfDay) {
		LocalTime time1 = LocalTime.now();
		LocalTime time2 = LocalTime.ofSecondOfDay(secondOfDay);
		int compare = time1CompareTotime2(time1, time2);
		if (compare == -1) {
			//当前时间未达到预定时间
			int currentSecond = time1.toSecondOfDay();
			int diff = secondOfDay - currentSecond;
			return diff;
		} else if (compare == 0) {
			//当前时间等于预定时间
			return 24 * 3600;
		} else if (compare == 1) {
			//当前时间已超过预定时间
			int currentSecond = time1.toSecondOfDay();
			int diff = secondOfDay - currentSecond;
			return 24 * 3600 + diff;
		}
		return 0;
	}
	
	public static boolean isTheSameYearMonth(int yyyyMMDateValue) {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		Integer currentyyyyMMValue = Integer.valueOf(String.valueOf(year + (++month)));
		if (currentyyyyMMValue == yyyyMMDateValue)
			return true;
		return false;
	}
	
	public static Long addSpecifyMinute(long timeInMills, int minute) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.setTimeInMillis(timeInMills);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTimeInMillis();
	}
	
	public static Integer getCurrentDayOfWeek(long currentTimeInMills) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.setTimeInMillis(currentTimeInMills);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	public static Long getNextWeekDateTime(int date) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.set(Calendar.DAY_OF_WEEK, date);
		calendar.set(Calendar.HOUR_OF_DAY, 8);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.WEEK_OF_YEAR, 1);
		return calendar.getTimeInMillis();
	}
	
	public static Long getCurrentWeekDateTime(int date) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.set(Calendar.DAY_OF_WEEK, date);
		calendar.set(Calendar.HOUR_OF_DAY, 8);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTimeInMillis();
	}
}
